<?php

namespace Biz\service\brand;


use Biz\model\brand\Brand as mBrand;
use Doraemon\pockets\constInterface\LinkConf;

class BrandHandleTask
{
    private static array $instance = [];
    private int $uid;
    private int $brandId;
    private array $error = [];
    private bool $isAward;
    private $taskId;
    private string $taskPrefix = '';
    /**
     * @method static levelTaskPublish(int $uid, int $brandId = 0, bool $add = true)
     * @method static levelTaskClaim(int $uid, int $brandId = 0, bool $add = true)
     * @method static levelTaskJoin(int $uid, int $brandId = 0, bool $add = true)
     * @method static dailyTaskPublish(int $uid, int $brandId = 0, bool $add = true)
     * @method static dailyTaskClaim(int $uid, int $brandId = 0, bool $add = true)
     * @method static dailyTaskClaimWiki(int $uid, int $brandId = 0, bool $add = true)
     * @method static dailyTaskFeatured(int $uid, int $brandId = 0, bool $add = true)
     * @method static dailyTaskLiked(int $uid, int $brandId = 0, bool $add = true)
     * @method static dailyTaskReviewed(int $uid, int $brandId = 0, bool $add = true)
     * @method static primaryTaskDone(int $uid, int $brandId = 0, bool $add = true)
     *
     */
    //任务类型 点赞、评论、认领、认领关联商品、发布内容、发布优质内容、设置精选内容
    const TASK_TYPE_LIKED = 'liked';
    const TASK_TYPE_REVIEWED = 'reviewed';
    const TASK_TYPE_CLAIM = 'claim';
    const TASK_TYPE_CLAIM_WIKI = 'claimWiki';
    const TASK_TYPE_PUBLISH = 'publish';
    const TASK_TYPE_QUALITY_PUBLISH = 'qualityPublish';
    const TASK_TYPE_FEATURED = 'featured';
    const TASK_PREFIX_TYPE_DAILY = 'daily_task';
    const TASK_PREFIX_TYPE_TIME = 'time_task';
    const TASK_TYPE_MAP = [
        self::TASK_TYPE_PUBLISH => '发布内容',
        self::TASK_TYPE_QUALITY_PUBLISH => '发布优质内容',
        self::TASK_TYPE_REVIEWED => '内容评论',
        self::TASK_TYPE_LIKED => '内容点赞',
        self::TASK_TYPE_CLAIM => '认领住友内容',
        self::TASK_TYPE_CLAIM_WIKI => '收录带商品链接的住友内容',
        self::TASK_TYPE_FEATURED => '设置精选内容',
    ];

    public static function admParseTaskType($taskType)
    {
        if (empty($taskType)) return;
        switch ($taskType) {
            case self::TASK_TYPE_QUALITY_PUBLISH:
            case self::TASK_TYPE_PUBLISH:
                $task_sub_title = 'app';
                $linkType = LinkConf::LINK_APP_EDITOR_CREATE;
                $linkParams = '';
                break;
            case self::TASK_TYPE_REVIEWED:
            case self::TASK_TYPE_LIKED:
                $task_sub_title = 'app|https://brand.haohaozhu.cn/content-pond/management';
                $linkType = LinkConf::LINK_HOME_TAG_INDEX;
                $linkParams = ['name' => ''];
                break;
            case self::TASK_TYPE_CLAIM:
            case self::TASK_TYPE_CLAIM_WIKI:
                $task_sub_title = 'brand';
                $linkType = LinkConf::LINK_APP_HTTPS;
                $linkParams = ['url' => 'https://brand.haohaozhu.cn/content-pond/claim'];
                break;
            case self::TASK_TYPE_FEATURED:
                $task_sub_title = 'brand';
                $linkType = LinkConf::LINK_APP_HTTPS;
                $linkParams = ['url' => 'https://brand.haohaozhu.cn/content-pond/management'];
                break;
            default:
                throw new \Exception("未知任务类型");
        }
        return [$task_sub_title, $linkType, $linkParams];
    }

    public static function getInstance($uid = 0, $brandId = 0): ?BrandHandleTask
    {
        $key = $uid . "_" . $brandId;
        if (empty(self::$instance[$key])) {
            self::$instance[$key] = new self($uid, $brandId);
        }
        return self::$instance[$key];
    }

    public function getUid(): int
    {
        return $this->uid;
    }

    public function __construct($uid, $brandId = 0, $type = self::TASK_PREFIX_TYPE_TIME)
    {
        $this->uid = $uid;
        $this->brandId = $brandId;
        $this->isAward = true;
        $this->taskPrefix = $type;
    }

    public function selectTaskType($type = self::TASK_PREFIX_TYPE_TIME)
    {
        $this->taskPrefix = $type;
    }

    public function selectTask(string $taskName): BrandHandleTask
    {
        try {
            $brandInfo = $this->uid > 0 ? mBrand::getBrandInfoByUid($this->uid) : mBrand::getInfo($this->brandId);
            if (empty($brandInfo)) throw new \Exception("不是品牌账号");
            if (empty($taskName)) throw new \Exception("任务名称不能为空");
            $this->taskId = \Doraemon\model\Task\TaskBase::getIdByName($this->taskPrefix, $taskName);
            if (empty($this->taskId)) throw new \Exception("任务名称[$taskName]不存在");
        } catch (\Exception $e) {
            $this->setErr($e->getMessage());
        }
        return $this;
    }

    public function disableAward(): BrandHandleTask
    {
        $this->isAward = false;
        return $this;
    }

    public function finish($finishedNum = 1)
    {
        try {
            $realFinishedNum = intval($finishedNum);
            if (empty($this->taskId)) throw new \Exception("没有完成的任务id");
            if ($realFinishedNum <= 1) $realFinishedNum = null;
            foreach ($this->taskId as $taskId) {
                \Doraemon\service\Task\Task::handleTask($this->uid, $taskId, $this->isAward, $realFinishedNum);
            }
        } catch (\Exception $e) {
            $this->setErr($e->getMessage());
        }
    }

    public function setErr($err): BrandHandleTask
    {
        $this->error[] = $err;
        return $this;
    }

    public function error(): array
    {
        return $this->error;
    }
}
